package net.neiquanlib.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;


import com.pizidea.imagepicker.ImageUtils;

import net.neiquanlib.R;

import java.util.List;

public class AddImageAdapter extends BaseAdapter implements View.OnClickListener {


    private Context context;
    private List<String> adImageUrl;
    private LayoutInflater mInflater;
    private int maxSize;

    /**
     *
     */
    public AddImageAdapter(List<String> adImageUrl, int maxSize, Context context) {
        this.context = context;
        this.adImageUrl = adImageUrl;
        this.maxSize = maxSize;
    }

    public void remove(String obj) {
/*		if (position<adImageUrl.size()) {
        }*/
        adImageUrl.remove(obj);
        notifyDataSetChanged();
    }

    public void refesh(List<String> adImageUrl) {
        this.adImageUrl = adImageUrl;
        notifyDataSetChanged();
    }

    // 取得适配器中的视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //将适配器中缓冲的视图控件返回

        PicModelHodler holder = null;
        if (convertView == null) {
            holder = new PicModelHodler();
            mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.grid_view_item_add, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.add_img);
            holder.del = (ImageView) convertView.findViewById(R.id.del_img);
            convertView.setTag(holder);
        } else {
            holder = (PicModelHodler) convertView.getTag();
        }
        String path = adImageUrl.get(position);
        if (position == adImageUrl.size() - 1 && path.startsWith(ImageUtils.IMAGE_DRAWABLE)) {
            //path = adImageUrl.get(position); // 这个是  + 号那个图片
        } else {
            path = ImageUtils.IMAGE_FILE + adImageUrl.get(position);
        }
        ImageUtils.LoadPicture(path, holder.imageView, R.drawable.pictures_no2);
        if (position == adImageUrl.size() - 1 && path.startsWith(ImageUtils.IMAGE_DRAWABLE)) {
            holder.del.setVisibility(View.GONE);
        } else {
            holder.del.setVisibility(View.VISIBLE);
        }
        holder.del.setTag(position);
        holder.del.setOnClickListener(this);
        return convertView;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public int getCount() {

        return adImageUrl.size();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.del_img) {// 如果没有加号
            int pos = Integer.parseInt(view.getTag().toString());
            adImageUrl.remove(pos);
            if (!adImageUrl.contains(ImageUtils.IMAGE_DRAWABLE + R.drawable.addpic)) {
                adImageUrl.add(ImageUtils.IMAGE_DRAWABLE + R.drawable.addpic);
            }
            notifyDataSetChanged();

        }
    }

    private final class PicModelHodler {
        public ImageView imageView;
        public ImageView del;
    }

}
